﻿Shader "Custom/Terrain2" {
	Properties {
		[HideInInspector] _Control ("Control (RGBA)", 2D) = "red" {}
		[HideInInspector] _Splat3 ("Layer 3 (A)", 2D) = "white" {}
		[HideInInspector] _Splat2 ("Layer 2 (B)", 2D) = "white" {}
		[HideInInspector] _Splat1 ("Layer 1 (G)", 2D) = "white" {}
		[HideInInspector] _Splat0 ("Layer 0 (R)", 2D) = "white" {}
		// used in fallback on old cards & base map
		// _MainTex ("BaseMap (RGB)", 2D) = "white" {}
		[HideInInspector] _Color ("Main Color", Color) = (1,1,1,1)
	}

		CGINCLUDE

		#pragma surface surf Lambert vertex:SplatmapVert finalcolor:SplatmapFinalColor
		//#pragma multi_compile_fog
		#include "TerrainSplatmapCommon.cginc"
		sampler2D _MainTex; 


		void SplatmapMix2(Input IN, out half4 splat_control, out half weight, out fixed4 mixedDiffuse)
		{
			splat_control = tex2D(_Control, IN.tc_Control);
			weight = dot(splat_control, half4(1,1,1,1));

			// Normalize weights before lighting and restore weights in final modifier functions so that the overal
			// lighting result can be correctly weighted.
			splat_control /= (weight + 1e-3f);

			mixedDiffuse = 0.0f;
			mixedDiffuse += splat_control.r * tex2D(_Splat0, IN.uv_Splat0);
			mixedDiffuse += splat_control.g * tex2D(_Splat1, IN.uv_Splat1);
			mixedDiffuse += splat_control.b * tex2D(_Splat2, IN.uv_Splat2);
			mixedDiffuse += splat_control.a * tex2D(_Splat3, IN.uv_Splat3);

			//fixed4 mainCol = tex2D(_MainTex, fixed2(0.5, 0.5));
			//mixedDiffuse = mainCol;

			//fixed4 mainCol = tex2D(_MainTex, IN.uv_Splat0);
			//mixedDiffuse = mainCol;
			/*
			mixedDiffuse = splat_control.r * tex2D(_Splat0, IN.uv_Splat0); 
			//mixedDiffuse = splat_control.g * tex2D(_Splat1, IN.uv_Splat1);

			//mixedDiffuse = splat_control.b * tex2D(_Splat2, IN.uv_Splat2);

			//mixedDiffuse = splat_control.a * tex2D(_Splat3, IN.uv_Splat3);

			mixedDiffuse = (1-splat_control.g) * mixedDiffuse + splat_control.g * tex2D(_Splat1, IN.uv_Splat1);
			mixedDiffuse = (1-splat_control.b) * mixedDiffuse + splat_control.b * tex2D(_Splat2, IN.uv_Splat2);
			mixedDiffuse = (1-splat_control.a) * mixedDiffuse + splat_control.a * tex2D(_Splat3, IN.uv_Splat3);
			*/
		}

		void surf(Input IN, inout SurfaceOutput o)
		{
			half4 splat_control;
			half weight;
			fixed4 mixedDiffuse;
			SplatmapMix2(IN, splat_control, weight, mixedDiffuse);

			o.Albedo = mixedDiffuse.rgb;
			o.Alpha = weight;
			//o.Alpha = 1;
		}

	ENDCG

	Category {
		Tags {
			"Queue" = "Geometry-99"
			"RenderType" = "Opaque"
		}
		//Blend SrcAlpha OneMinusSrcAlpha

		// TODO: Seems like "#pragma target 3.0 _TERRAIN_NORMAL_MAP" can't fallback correctly on less capable devices?
		// Use two sub-shaders to simulate different features for different targets and still fallback correctly.
		SubShader { // for sm3.0+ targets
			CGPROGRAM
				#pragma target 3.0
				//#pragma multi_compile __ _TERRAIN_NORMAL_MAP
			ENDCG
		}
		SubShader { // for sm2.0 targets
			CGPROGRAM

			ENDCG
		}
	}

	Dependency "AddPassShader" = "Hidden/TerrainEngine/Splatmap/Diffuse-AddPass"
	Dependency "BaseMapShader" = "Diffuse"
	Dependency "Details0"      = "Hidden/TerrainEngine/Details/Vertexlit"
	Dependency "Details1"      = "Hidden/TerrainEngine/Details/WavingDoublePass"
	Dependency "Details2"      = "Hidden/TerrainEngine/Details/BillboardWavingDoublePass"
	Dependency "Tree0"         = "Hidden/TerrainEngine/BillboardTree"

	Fallback "Diffuse"
}
